package com.lyf.scm.admin.domain.util;

import com.lyf.scm.common.constants.CommonConstants;
import com.lyf.scm.common.model.BaseSearchDTO;
import com.lyf.scm.common.model.SalesPlanSelectDTO;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * description
 *
 * @author wangchlei
 * @date 2020/08/11
 */
public class BaseSearchUtil {


    /**
     * 设置查询属性
     *
     * @param channelSalesPlanSelects
     * @param categorySalesPlanSelects
     * @param areaSalesPlanSelects
     * @return void
     **/
    public static void setSearchFields(BaseSearchDTO baseSearchDTO, List<SalesPlanSelectDTO> channelSalesPlanSelects, List<SalesPlanSelectDTO> categorySalesPlanSelects, List<SalesPlanSelectDTO> areaSalesPlanSelects) {
        //渠道查询条件转换设置
        if (!CollectionUtils.isEmpty(baseSearchDTO.getChannelCodeList())) {
            if (!CollectionUtils.isEmpty(channelSalesPlanSelects)) {
                List<String> channelCodes = channelSalesPlanSelects.stream().map(SalesPlanSelectDTO::getCode).collect(Collectors.toList());
                if (CommonConstants.CHANNEL_LEVEL_ONE.equals(channelSalesPlanSelects.get(0).getLevel())) {
                    // baseSearchDTO.setFatherChannelCodeList(channelCodes);
                }
                if (CommonConstants.CHANNEL_LEVEL_TOW.equals(channelSalesPlanSelects.get(0).getLevel())) {
                    // baseSearchDTO.setSubChannelCodeList(channelCodes);
                }
            }
        }
        //品类查询条件设置
        if (!CollectionUtils.isEmpty(baseSearchDTO.getCategoryCodeList())) {
            if (!CollectionUtils.isEmpty(categorySalesPlanSelects)) {
                List<String> categoryCodes = categorySalesPlanSelects.stream().map(SalesPlanSelectDTO::getCode).collect(Collectors.toList());
                if (CommonConstants.CATEGORY_LEVEL_ONE.equals(categorySalesPlanSelects.get(0).getLevel())) {
                    baseSearchDTO.setPriCategoryCodeList(categoryCodes);
                }
                if (CommonConstants.CATEGORY_LEVEL_TWO.equals(categorySalesPlanSelects.get(0).getLevel())) {
                    baseSearchDTO.setSecCategoryCodeList(categoryCodes);
                }
                if (CommonConstants.CATEGORY_LEVEL_THREE.equals(categorySalesPlanSelects.get(0).getLevel())) {
                    baseSearchDTO.setTerCategoryCodeList(categoryCodes);
                }
                if (CommonConstants.CATEGORY_LEVEL_FOUR.equals(categorySalesPlanSelects.get(0).getLevel())) {
                    baseSearchDTO.setSkuCodeList(categoryCodes);
                }
            }
        }
        //地域查询条件设置
        if (!CollectionUtils.isEmpty(baseSearchDTO.getAreaCodeList())) {
            if (!CollectionUtils.isEmpty(areaSalesPlanSelects)) {
                List<String> areaCodes = areaSalesPlanSelects.stream().map(SalesPlanSelectDTO::getCode).collect(Collectors.toList());
                setAreaInfo(areaCodes, areaSalesPlanSelects, baseSearchDTO);
            }
        }
    }

    /**
     * 将区域单独提出是为了直营需求，必须对角色所在区域校验
     *
     * @param areaCodes
     * @param areaSalesPlanSelects
     * @param baseSearchDTO
     * @return void
     **/
    public static void setAreaInfo(List<String> areaCodes, List<SalesPlanSelectDTO> areaSalesPlanSelects, BaseSearchDTO baseSearchDTO) {
        if (CommonConstants.AREA_LEVEL_ONE.equals(areaSalesPlanSelects.get(0).getLevel())) {
            baseSearchDTO.setProvinceCodeList(areaCodes);
        }
        if (CommonConstants.AREA_LEVEL_TWO.equals(areaSalesPlanSelects.get(0).getLevel())) {
            baseSearchDTO.setCityCodeList(areaCodes);
        }
        if (CommonConstants.AREA_LEVEL_THREE.equals(areaSalesPlanSelects.get(0).getLevel())) {
            baseSearchDTO.setCountyCodeList(areaCodes);
        }
        if (CommonConstants.AREA_LEVEL_FOUR.equals(areaSalesPlanSelects.get(0).getLevel())) {
            baseSearchDTO.setStoreCodeList(areaCodes);
        }
    }

    /**
     * 设置查询属性
     *
     * @param channelSalesPlanSelects
     * @param categorySalesPlanSelects
     * @param areaSalesPlanSelects
     * @return void
     **/
    public static void setSearchField(BaseSearchDTO baseSearchDTO, List<SalesPlanSelectDTO> channelSalesPlanSelects, List<SalesPlanSelectDTO> categorySalesPlanSelects, List<SalesPlanSelectDTO> areaSalesPlanSelects) {
        //渠道查询条件转换设置
        if (!CollectionUtils.isEmpty(baseSearchDTO.getChannelCodeList())) {
            if (!CollectionUtils.isEmpty(channelSalesPlanSelects)) {
                List<String> channelCodeOne = new ArrayList<>();
                List<String> channelCodeTwo = new ArrayList<>();
                for (SalesPlanSelectDTO dto : channelSalesPlanSelects) {
                    if (CommonConstants.CHANNEL_LEVEL_ONE.equals(dto.getLevel())) {
                        channelCodeOne.add(dto.getCode());
                    } else {
                        channelCodeTwo.add(dto.getCode());
                    }
                }
                if (channelCodeOne.size() > 0) {
                    // baseSearchDTO.setFatherChannelCodeList(channelCodeOne);
                }
                if (channelCodeTwo.size() > 0) {
                    // baseSearchDTO.setSubChannelCodeList(channelCodeTwo);
                }
            }
        }
        //品类查询条件设置
        if (!CollectionUtils.isEmpty(baseSearchDTO.getCategoryCodeList())) {
            if (!CollectionUtils.isEmpty(categorySalesPlanSelects)) {
                List<String> categoryCodeOne = new ArrayList<>();
                List<String> categoryCodeTwo = new ArrayList<>();
                List<String> categoryCodeThree = new ArrayList<>();
                List<String> categoryCodeFour = new ArrayList<>();
                for (SalesPlanSelectDTO dto : categorySalesPlanSelects) {
                    if (CommonConstants.CATEGORY_LEVEL_ONE.equals(dto.getLevel())) {
                        categoryCodeOne.add(dto.getCode());
                    } else if (CommonConstants.CATEGORY_LEVEL_TWO.equals(dto.getLevel())) {
                        categoryCodeTwo.add(dto.getCode());
                    } else if (CommonConstants.CATEGORY_LEVEL_THREE.equals(dto.getLevel())) {
                        categoryCodeThree.add(dto.getCode());
                    } else if (CommonConstants.CATEGORY_LEVEL_FOUR.equals(dto.getLevel())) {
                        categoryCodeFour.add(dto.getCode());
                    }
                }
                if (categoryCodeOne.size() > 0) {
                    baseSearchDTO.setPriCategoryCodeList(categoryCodeOne);
                }
                if (categoryCodeTwo.size() > 0) {
                    baseSearchDTO.setSecCategoryCodeList(categoryCodeTwo);
                }
                if (categoryCodeThree.size() > 0) {
                    baseSearchDTO.setTerCategoryCodeList(categoryCodeThree);
                }
                if (categoryCodeFour.size() > 0) {
                    baseSearchDTO.setSkuCodeList(categoryCodeFour);
                }
            }
        }
        //地域查询条件设置
        if (!CollectionUtils.isEmpty(baseSearchDTO.getAreaCodeList())) {
            if (!CollectionUtils.isEmpty(areaSalesPlanSelects)) {
                List<String> areaCodeOne = new ArrayList<>();
                List<String> areaCodeTwo = new ArrayList<>();
                List<String> areaCodeThree = new ArrayList<>();
                List<String> areaCodeFour = new ArrayList<>();
                for (SalesPlanSelectDTO dto : areaSalesPlanSelects) {
                    if (CommonConstants.AREA_LEVEL_ONE.equals(dto.getLevel())) {
                        areaCodeOne.add(dto.getCode());
                    } else if (CommonConstants.AREA_LEVEL_TWO.equals(dto.getLevel())) {
                        areaCodeTwo.add(dto.getCode());
                    } else if (CommonConstants.AREA_LEVEL_THREE.equals(dto.getLevel())) {
                        areaCodeThree.add(dto.getCode());
                    } else if (CommonConstants.AREA_LEVEL_FOUR.equals(dto.getLevel())) {
                        areaCodeFour.add(dto.getCode());
                    }
                }
                if (areaCodeOne.size() > 0) {
                    baseSearchDTO.setProvinceCodeList(areaCodeOne);
                }
                if (areaCodeTwo.size() > 0) {
                    baseSearchDTO.setCityCodeList(areaCodeTwo);
                }
                if (areaCodeThree.size() > 0) {
                    baseSearchDTO.setCountyCodeList(areaCodeThree);
                }
                if (areaCodeFour.size() > 0) {
                    baseSearchDTO.setStoreCodeList(areaCodeFour);
                }
            }
        }
    }


    /**
     * 导出时候按照勾选框条件进行参数设置
     *
     * @param salePlanDirectlyDifferinDTO
     * @param rowParamList
     * @return void
     **/
//    public static void setSearchFields(SalePlanDirectlyDifferinDTO salePlanDirectlyDifferinDTO, List<RowParamDTO> rowParamList) {
//        if (!CollectionUtils.isEmpty(rowParamList)) {
//            RowParamDTO firstRowParamDTO = rowParamList.get(0);
//            if (!StringUtils.isEmpty(firstRowParamDTO.getChannelCode())) {
//                salePlanDirectlyDifferinDTO.setFatherChannelCodeList(rowParamList.stream().map(RowParamDTO::getChannelCode).distinct().collect(Collectors.toList()));
//            }
//            if (!StringUtils.isEmpty(firstRowParamDTO.getSubChannelCode())) {
//                salePlanDirectlyDifferinDTO.setFatherChannelCodeList(null);
//                salePlanDirectlyDifferinDTO.setSubChannelCodeList(rowParamList.stream().map(RowParamDTO::getSubChannelCode).distinct().collect(Collectors.toList()));
//            }
//            if (!StringUtils.isEmpty(firstRowParamDTO.getProvinceCode())) {
//                salePlanDirectlyDifferinDTO.setProvinceCodeList(rowParamList.stream().map(RowParamDTO::getProvinceCode).distinct().collect(Collectors.toList()));
//            }
//            if (!StringUtils.isEmpty(firstRowParamDTO.getCityCode())) {
//                salePlanDirectlyDifferinDTO.setProvinceCodeList(null);
//                salePlanDirectlyDifferinDTO.setCityCodeList(rowParamList.stream().map(RowParamDTO::getCityCode).distinct().collect(Collectors.toList()));
//            }
//            if (!StringUtils.isEmpty(firstRowParamDTO.getCountyCode())) {
//                salePlanDirectlyDifferinDTO.setProvinceCodeList(null);
//                salePlanDirectlyDifferinDTO.setCityCodeList(null);
//                salePlanDirectlyDifferinDTO.setCountyCodeList(rowParamList.stream().map(RowParamDTO::getCountyCode).distinct().collect(Collectors.toList()));
//            }
//            if (!StringUtils.isEmpty(firstRowParamDTO.getStoreCode())) {
//                salePlanDirectlyDifferinDTO.setProvinceCodeList(null);
//                salePlanDirectlyDifferinDTO.setCityCodeList(null);
//                salePlanDirectlyDifferinDTO.setCountyCodeList(null);
//                salePlanDirectlyDifferinDTO.setStoreCodeList(rowParamList.stream().map(RowParamDTO::getStoreCode).distinct().collect(Collectors.toList()));
//            }
//            if (!StringUtils.isEmpty(firstRowParamDTO.getPriCategoryCode())) {
//                salePlanDirectlyDifferinDTO.setPriCategoryCodeList(rowParamList.stream().map(RowParamDTO::getPriCategoryCode).distinct().collect(Collectors.toList()));
//            }
//            if (!StringUtils.isEmpty(firstRowParamDTO.getSecCategoryCode())) {
//                salePlanDirectlyDifferinDTO.setPriCategoryCodeList(null);
//                salePlanDirectlyDifferinDTO.setSecCategoryCodeList(rowParamList.stream().map(RowParamDTO::getSecCategoryCode).distinct().collect(Collectors.toList()));
//            }
//            if (!StringUtils.isEmpty(firstRowParamDTO.getTerCategoryCode())) {
//                salePlanDirectlyDifferinDTO.setPriCategoryCodeList(null);
//                salePlanDirectlyDifferinDTO.setSecCategoryCodeList(null);
//                salePlanDirectlyDifferinDTO.setTerCategoryCodeList(rowParamList.stream().map(RowParamDTO::getTerCategoryCode).distinct().collect(Collectors.toList()));
//            }
//            if (!StringUtils.isEmpty(firstRowParamDTO.getSkuCode())) {
//                salePlanDirectlyDifferinDTO.setPriCategoryCodeList(null);
//                salePlanDirectlyDifferinDTO.setSecCategoryCodeList(null);
//                salePlanDirectlyDifferinDTO.setTerCategoryCodeList(null);
//                salePlanDirectlyDifferinDTO.setSkuCodeList(rowParamList.stream().map(RowParamDTO::getSkuCode).distinct().collect(Collectors.toList()));
//            }
//        }
//
//    }
}
